package com.cq.hd.order.render.impl;

import com.cq.hd.order.api.dto.CreateOrderDto;
import com.cq.hd.order.render.OrderContext;
import com.cq.hd.order.render.OrderRenderStepEnum;
import com.cq.hd.order.render.dto.PriceDetailDto;
import com.cq.hd.product.api.vo.ActiveVo;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * 订单价格计算
 *
 * @author much
 * @since 2024/09/13 22:04
 */
@Component
public class OrderPriceRender extends AbstractRenderStep {

    @Override
    public OrderRenderStepEnum step() {
        return OrderRenderStepEnum.ORDER_PRICE;
    }

    @Override
    public void render(OrderContext orderContext) {
        CreateOrderDto createOrderDto = orderContext.getCreateOrderDto();
        ActiveVo activeVo = orderContext.getActiveVo();
        PriceDetailDto priceDetailDto = orderContext.getPriceDetailDTO();

        // orderPrice
        BigDecimal activePrice = priceDetailDto.getActivePrice();
        BigDecimal orderPrice = activePrice.multiply(new BigDecimal(String.valueOf(createOrderDto.getOrderNum())));
        priceDetailDto.setOrderPrice(orderPrice);

        // payPrice
        BigDecimal discount = priceDetailDto.getDiscountRate();
        BigDecimal discountAmt = discount == null ? BigDecimal.ZERO : orderPrice.subtract(orderPrice.multiply(discount));
        priceDetailDto.setDiscountAmt(discountAmt);
        // 实付金额=订单总额-红包金额-优惠金额
        BigDecimal redPacketAmt = priceDetailDto.getRedPacketAmt();
        BigDecimal payPrice = orderPrice.subtract(redPacketAmt).subtract(discountAmt);
        if (payPrice.compareTo(BigDecimal.ZERO) <= 0) {
            payPrice = BigDecimal.ZERO;
        }
        priceDetailDto.setPayPrice(payPrice);

    }

}
